package com.baichang.taoshunda.controller.mobile;

import com.baichang.framework.controller.BaseMobileController;
import com.baichang.framework.controller.IService;
import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.controller.ControllerConstants;
import com.baichang.taoshunda.service.BusinessService;
import com.baichang.taoshunda.service.BussGoodsTypeService;
import com.baichang.taoshunda.service.FavoritesService;
import com.baichang.taoshunda.service.GoodsCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * Created by ${wushenglu} on 2017/8/22 0022.
 */
@Controller
@RequestMapping(ControllerConstants.BUSSINE)
public class BussinessMCtronller extends BaseMobileController {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private BussGoodsTypeService bussGoodsTypeService;
    @Autowired
    private GoodsCommentService goodsCommentService;
    @Autowired
    private FavoritesService favoritesService;

    /**
     * 查询商家列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getBussinessList")
    public void getBussinessList(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int cityId = getInt(s, "cityId", 0);
                int typeId = getInt(s, "typeId", 1);
                int userId = getInt(s, "userId", 1);
                int bussTypeId = getInt(s, "bussTypeId", 0);
                double ulng = getDouble(s, "ulng", 0);
                double ulat = getDouble(s, "ulat", 0);
                int nowPage = getInt(s, "nowPage", 1);
                int pageSize = getInt(s, "pageSize", 20);
                return businessService.getBussinessList(cityId, ulat, ulng, typeId, nowPage, pageSize, bussTypeId, userId);
            }
        });
    }


    /**
     * 根据用户id查询邀请商家列表
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getBussinessListByUserId")
    public void getBussinessListByUserId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int userId = getInt(s, "userId", 1);
                double ulng = getDouble(s, "ulng", 0);
                double ulat = getDouble(s, "ulat", 0);
                int nowPage = getInt(s, "nowPage", 1);
                int pageSize = getInt(s, "pageSize", 20);
                return businessService.getBussinessListByUserId(userId, ulat, ulng, nowPage, pageSize);
            }
        });
    }

    /**
     * 查询商家详情
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getBussinessDateial")
    public void getBussinessDateial(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int bussId = getInt(s, "bussId", 0);
                int userId = getInt(s, "userId", 0);
                double ulng = getDouble(s, "ulng", 0);
                double ulat = getDouble(s, "ulat", 0);
                return businessService.getBussinessDateial(bussId, ulat, ulng, userId);
            }
        });
    }

    /**
     * 查询商家下的商品分类
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getGoodsTypeByBussId")
    public void getGoodsTypeByBussId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int bussId = getInt(s, "bussId", 0);
                int userId = getInt(s, "userId", 0);
                return bussGoodsTypeService.getGoodsTypeByBussId(bussId, userId);
            }
        });
    }

    /**
     * 商家的评论
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getCommentByBussId")
    public void getCommentByBussId(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int bussId = getInt(s, "goodsId", 0);
                int nowPage = getInt(s, "nowPage", 1);
                int pageSize = getInt(s, "pageSize", 20);
                return goodsCommentService.getCommentByBussId(bussId, nowPage, pageSize);
            }
        });
    }

    /**
     * 收藏商家
     *
     * @param request
     * @param response
     */
    @RequestMapping("/favoriterBuss")
    public void favoriterBuss(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int bussId = getInt(s, "bussId", 0);
                int userId = getInt(s, "userId", 0);
                int typeId = getInt(s, "typeId", 1);
                return favoritesService.favoriterBuss(bussId, userId, typeId);
            }
        });
    }

    /**
     * 搜索商家
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getBusinessByKey")
    public void getBusinessByKey(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                int cityId = getInt(s, "cityId", 0);
                String key = getString(s, "key", "");
                double ulng = getDouble(s, "ulng", 0);
                int bussTypeId = getInt(s, "bussTypeId", 0);
                double ulat = getDouble(s, "ulat", 0);
                int nowPage = getInt(s, "nowPage", 1);
                int pageSize = getInt(s, "pageSize", 20);
                return businessService.getBusinessByKey(cityId, key, ulat, ulng, bussTypeId, nowPage, pageSize);
            }
        });
    }

    /**
     * 获取用户收货地址到商家的配送费
     *
     * @param request
     * @param response
     */
    @RequestMapping("/getDispatching")
    public void getDispatching(HttpServletRequest request, HttpServletResponse response) {
        initEncode(request, response, new IService() {
            @Override
            public Object init(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String s) throws ServiceException {
                double ulng = getDouble(s, "ulng", 0);
                double ulat = getDouble(s, "ulat", 0);
                int bussId = getInt(s, "bussId", 0);
                int isFree = getInt(s, "isFree", 0);
                return businessService.getDispatching(ulat, ulng, bussId, isFree);
            }
        });
    }


}
